const fs = require('fs');

// 把 products.json 原始数据格式化
const processData1 = () => {
  const products = require('./products.json');
  const formatedPrts = products.map((obj) => {
    if (
      obj.fields.item &&
      obj.fields.item.name &&
      obj.fields.style &&
      obj.fields.style.name &&
      obj.fields.originalPrice
    )
      return {
        id: obj.id,
        item: obj.fields.item.name,
        style: obj.fields.style.name,
        price: obj.fields.originalPrice,
      };
  });
  fs.writeFileSync('formatedPrcts.json', JSON.stringify(formatedPrts));
};

const processData2 = () => {
  const formatedPrcts = require('./formatedPrcts.json');
  const data = formatedPrcts.map((obj) => {
    if (obj && obj.item && obj.style && obj.price) {
      const itemArr = obj.item.split(' ');
      const item = itemArr[1] + itemArr[0];
      const styleArr = obj.style.split(' ');
      const style = styleArr[1] + styleArr[0];
      const price = obj.price;
      return `${item},${style},${price}`;
    }
  });
  fs.writeFileSync('products.txt', data.join('\n'));
};

const processData3 = () => {
  const formatedPrcts = require('./formatedPrcts.json');
  const data = formatedPrcts.map((obj) => {
    if (obj && obj.item && obj.style && obj.price) {
      const itemArr = obj.item.split(' ');
      const item = itemArr[1] + itemArr[0];
      const styleArr = obj.style.split(' ');
      const style = styleArr[1] + styleArr[0];
      const price = obj.price;
      return {
        id: obj.id,
        item,
        itemStr: obj.item,
        style,
        price,
      };
    }
  });
  fs.writeFileSync('simplePrcts.json', JSON.stringify(data));
};

const processData4 = () => {
  const items = [
    'LV327',
    'LV300',
    'LV451',
    'LV326',
    'LV2664',
    'LV453',
    'LV205',
    'LV33',
    'LV343',
    'LV1558',
    'LV262',
  ];
  const products = require('./simplePrcts.json');

  for (const item of items) {
    const data = [];
    for (const product of products) {
      if (product && product.item && item == product.item) {
        data.push(`${product.item},\t${product.price},\t${product.itemStr}`);
      }
    }
    fs.writeFileSync(`${item}.txt`, data.join('\n'));
  }
};

function main() {
  // processData1();
  // processData2();
  // processData3();
  // processData4();
}

main();
